package top.cardone.validator.func;

import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * Created by Administrator on 2016/9/19.
 */
@Log4j2
public class MinLengthValidatorFunc extends RequiredValidatorFunc {
	public MinLengthValidatorFunc() {
		this.code = "validator.minlength";
	}

	@Override
	public String func(Map<String, Object> map, String key, Object rule, Object message) {
		log.debug(this.code + ":" + key);

		String val = MapUtils.getString(map, key);

		if (StringUtils.isBlank(val)) {
			return null;
		}

		int minLength = ((Double) rule).intValue();

		if (StringUtils.length(val) >= minLength) {
			return null;
		}

		return this.getMessage(message, new Object[]{minLength});
	}
}